﻿
EXEC sp_configure filestream_access_level, 2
RECONFIGURE
GO

CREATE DATABASE FileBase2008
ON
PRIMARY ( NAME = Arch1,
    FILENAME = 'e:\_data\FileBase2008.mdf'),
FILEGROUP FileStreamGroup1 CONTAINS FILESTREAM ( 
	NAME = Arch3, 
	FILENAME = 'e:\_data\FileBase2008_FileStream')
	LOG ON  ( 
	NAME = Archlog1, 
	FILENAME = 'e:\_data\FileBase2008.ldf')
GO

USE FileBase2008

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FileStorage]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
	drop table [dbo].[FileStorage]
GO

CREATE TABLE dbo.FileStorage
(
	[FileStorageId] [UNIQUEIDENTIFIER] ROWGUIDCOL NOT NULL UNIQUE, 
	[ContentLength] INTEGER NULL,
	[FileName] VARCHAR(255) NULL,
	[MimeType] VARCHAR(255) NULL,
	[FileStream] VARBINARY(MAX) FILESTREAM NULL
)
GO


ALTER TABLE [dbo].[FileStorage] ADD  CONSTRAINT [DF_FileStorage_FileStorageId]  DEFAULT (newid()) FOR [FileStorageId]
GO

if not exists (select * from master.dbo.syslogins where loginname = N'webuser')
BEGIN
	declare @logindb nvarchar(132), @loginlang nvarchar(132) select @logindb = N'master', @loginlang = N'us_english'
	if @logindb is null or not exists (select * from master.dbo.sysdatabases where name = @logindb)
		select @logindb = N'master'
	if @loginlang is null or (not exists (select * from master.dbo.syslanguages where name = @loginlang) and @loginlang <> N'us_english')
		select @loginlang = @@language
	exec sp_addlogin N'webuser', null, @logindb, @loginlang
END
GO

if not exists (select * from dbo.sysusers where name = N'webuser' and uid < 16382)
EXEC sp_grantdbaccess N'webuser', N'webuser'
GO

GRANT  SELECT ,  UPDATE ,  INSERT ,  DELETE  ON [dbo].[FileStorage]  TO [webuser]
GO

